﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CK.Plugin;
using ContribServices;
using System.Diagnostics;
using GesturePlugin;

namespace GestureActionPlugin
{

    [Plugin(Id = "{3B812EB0-D02F-4cad-9F29-F11AD0EC2CAE}", PublicName = "Gesture action", Version = "1.0",
     DefaultPluginStatus = ConfigPluginStatus.Manual, Categories = new string[] { "Advanced" },
     Description = "Associate Gesture with an action")]

    public class GestureActionPlugin : IPlugin
    {
        private IGesturePlugin _gesturePlugin;
        private ISystemEventWatcher _systemEventWatcher;
        private GestureActionStrategyManager _gestureActionStrategyManager;

        /// <summary>
        /// Access to the GesturePlugin
        /// </summary>
        [Service(Requires = ServiceRequirements.MustExistAndRun)]
        public IGesturePlugin GesturePlugin
        {
            get { return _gesturePlugin; }
            set { _gesturePlugin = value; }
        }

        /// <summary>
        /// Access to the SystemEventWatcher
        /// </summary>
        [Service(Requires = ServiceRequirements.MustExistAndRun)]
        public ISystemEventWatcher SystemEventWatcher
        {
            get { return _systemEventWatcher; }
            set { _systemEventWatcher = value; }
        }

        #region IPlugin Members

        public bool CanStart(out string lastError)
        {
            lastError = "";
            return true;
        }

        public bool Setup(ISetupInfo info)
        {
            return true;
        }

        public void Start()
        {
            GesturePlugin.GestureMatched += new GesturePluginEventHandler(GestureMatched);
            _gestureActionStrategyManager = new GestureActionStrategyManager();
            _gestureActionStrategyManager.GestureActionStategy = new DesktopStrategy();

            SystemEventWatcher.ChangeForegroundWindow += new ChangeForegroundWindowEventHandler(UpdateStrategy);
        }

        public void Stop()
        {
        }

        public void Teardown()
        {
        }

        #endregion
        
        /// <summary>
        /// Calls when the SystemEventWatcher fires its event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void UpdateStrategy(object sender, ChangeForegroundWindowEventArgs e)
        {
            Process currentProcessById = Process.GetProcessById(e.processID);
            string currentProcessName = currentProcessById.ProcessName;
            Debug.WriteLine(currentProcessName);

            switch (currentProcessName)
            {
                case "explorer":
                    _gestureActionStrategyManager.GestureActionStategy = new DesktopStrategy();
                    break;
                case "WINWORD":
                    _gestureActionStrategyManager.GestureActionStategy = new FirefoxStrategy();
                    break;
                default:
                    _gestureActionStrategyManager.GestureActionStategy = new DesktopStrategy();
                    break;
            }
        }

        /// <summary>
        /// Calls when the GesturePlugin fires its event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void GestureMatched(object sender, GesturePluginEventArgs e)
        {
            Debug.WriteLine(e.Gesture);
            _gestureActionStrategyManager.ExecuteAction(e.Gesture);
        }
    }
}
